package org.tgcloud.group.web.controller.manager;

import com.qiniu.util.StringUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.tgcloud.group.core.APPResultMessage;
import org.tgcloud.group.core.StringUtil;
import org.tgcloud.group.core.custom.Permissions;
import org.tgcloud.group.core.session.Session;
import org.tgcloud.group.service.manager.PermissionManagerService;

/**
 * @author: zhy
 * @create: 2023-07-17 13:28
 * @description: 后台权限管理相关
 */
@Api(tags = "后台权限管理相关")
@RestController
@RequestMapping("/manager")
public class PermissionManagerController extends ManageAPPBaseController{

    @Autowired
    private PermissionManagerService managerService;


    @ApiOperation(value = "登录获取对应的权限")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "token", value = "token", required = true, dataType = "string")
    })
    @ResponseBody
    @RequestMapping(value = "/pc/user/getPermissionList", method = RequestMethod.POST)
    public APPResultMessage getUserPermissionList(@ModelAttribute("session") Session session) {
        return managerService.getUserPermissionList(session.getUserId(), session.getComId());
    }

    @Permissions("managerUser:permissionEdit")
    @ApiOperation(value = "分配用户权限")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "token", value = "token", required = true, dataType = "string"),
            @ApiImplicitParam(name = "userId", value = "userId", required = true, dataType = "int"),
            @ApiImplicitParam(name = "powerIds", value = "powerIds", required = true, dataType = "string")
    })
    @ResponseBody
    @RequestMapping(value = "/pc/user/upPermission", method = RequestMethod.POST)
    public APPResultMessage doUpPermission(@ModelAttribute("session") Session session,
                                           int userId,
                                           String powerIds,
                                           @RequestParam(required = false) Short userType) {
        if (StringUtils.isNullOrEmpty(powerIds)) {
            return APPResultMessage.MISS_PARAMETER();
        }
        return managerService.doUpPermission(session.getUserId(), userId, powerIds,userType);
    }

    //    @Permissions("managerUser:editRole")
    @ApiOperation(value = "新增用户的角色列表")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "token", value = "token", required = true, dataType = "string")
    })
    @ResponseBody
    @RequestMapping(value = "/pc/user/getEditUserRoleList", method = RequestMethod.POST)
    public APPResultMessage doUpRolePermission(@ModelAttribute("session") Session session) {
        return managerService.getEditUserRoleList();
    }

    //    @Permissions("managerUser:permissionView")
    @ApiOperation(value = "个人权限列表")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "token", value = "token", required = true, dataType = "string")
    })
    @ResponseBody
    @RequestMapping(value = "/pc/user/getUserPermissionList", method = RequestMethod.POST)
    public APPResultMessage getPermissionList(@ModelAttribute("session") Session session,
                                              Integer userId,
                                              @RequestParam(required = false) Short userType) {
        return managerService.getPermissionList(session.getUserId(), userId,userType);
    }


    @Permissions("role:view")
    @ApiOperation(value = "角色列表")
    @ResponseBody
    @RequestMapping(value = "/pc/user/roleList", method = RequestMethod.POST)
    public APPResultMessage getRoleList(String keyword,
                                        Integer current,
                                        Integer pageSize,
                                        Long startTime,
                                        Long endTime,
                                        Short sort) {
        return managerService.getRoleList(keyword, current, pageSize, startTime, endTime, sort);
    }



    @Permissions("role:edit,role:edit")
    @ApiOperation(value = "新增修改角色")
    @ResponseBody
    @RequestMapping(value = "/pc/user/upRole", method = RequestMethod.POST)
    public APPResultMessage doAddUpRole(String roleName, Integer roleId,
                                        Short viewAll, String customName, Short multipleType) {
        if (StringUtil.usernameIllegal(roleName)) {
            return APPResultMessage.getErrorMessage("角色名必须是2-11位之间的汉字、字母、数字、下划线");
        }
        return managerService.doAddUpRole(roleName, roleId, viewAll, customName, multipleType);
    }


    @Permissions("role:del")
    @ApiOperation(value = "批量删除角色")
    @ResponseBody
    @RequestMapping(value = "/pc/user/delRole", method = RequestMethod.POST)
    public APPResultMessage doDelRole(String ids) {
        if (StringUtils.isNullOrEmpty(ids)) {
            return APPResultMessage.MISS_PARAMETER();
        }
        return managerService.doDelRole(ids);
    }

    @Permissions("role:del")
    @ApiOperation(value = "启用禁用角色")
    @ResponseBody
    @RequestMapping(value = "/pc/user/doDisenableRole", method = RequestMethod.POST)
    public APPResultMessage doDelRole(Integer id, Short type) {
        if (id == null) {
            return APPResultMessage.MISS_PARAMETER();
        }
        if (type.intValue() != 1 && type.intValue() != 3) {
            return APPResultMessage.MISS_PARAMETER();
        }
        return managerService.doDisenableRole(id, type);
    }


    //    @Permissions("role:permissionView")
    @ApiOperation(value = "角色权限列表")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "token", value = "token", required = true, dataType = "string"),
            @ApiImplicitParam(name = "roleId", value = "角色id", required = true, dataType = "int")
    })
    @ResponseBody
    @RequestMapping(value = "/pc/user/getRolePermissionList", method = RequestMethod.POST)
    public APPResultMessage getRolePermissionList(@ModelAttribute("session") Session session,
                                                  int roleId) {
        return managerService.getRolePermissionList(session.getUserId(), roleId);
    }


    @Permissions("role:permissionEdit")
    @ApiOperation(value = "分配角色权限")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "token", value = "token", required = true, dataType = "string"),
            @ApiImplicitParam(name = "roleId", value = "roleId", required = true, dataType = "int"),
            @ApiImplicitParam(name = "powerIds", value = "powerIds", required = true, dataType = "string")
    })
    @ResponseBody
    @RequestMapping(value = "/pc/user/upRolePermission", method = RequestMethod.POST)
    public APPResultMessage doUpRolePermission(@ModelAttribute("session") Session session,
                                               int roleId,
                                               String powerIds) {
        if (StringUtils.isNullOrEmpty(powerIds)) {
            return APPResultMessage.MISS_PARAMETER();
        }
        return managerService.doUpRolePermission(roleId, powerIds);
    }

    @ApiOperation(value = "所有角色列表，筛选使用")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "token", value = "token", required = true, dataType = "string")
    })
    @ResponseBody
    @RequestMapping(value = "/pc/user/getAllRoleList", method = RequestMethod.POST)
    public APPResultMessage getAllRoleList() {
        return managerService.getAllRoleList();
    }

    @ApiOperation(value = "查询出需要自定义的角色")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "token", value = "token", required = true, dataType = "string")
    })
    @ResponseBody
    @RequestMapping(value = "/pc/user/getAllCustomRoleList", method = RequestMethod.POST)
    public APPResultMessage getAllCustomRoleList() {
        return managerService.getAllCustomRoleList();
    }

    @ApiOperation(value = "根据角色查询出账户列表")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "token", value = "token", required = true, dataType = "string")
    })
    @ResponseBody
    @RequestMapping(value = "/pc/user/getAccountListByRoleIds", method = RequestMethod.POST)
    public APPResultMessage getAccountListByRoleIds(@RequestParam(required = false) String roleIds) {
        return managerService.getAccountListByRoleIds(roleIds);
    }




}
